﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace iSmartCreatorApp
{
    /// <summary>
    /// Interaction logic for DirectoryNameWindow.xaml
    /// </summary>
    public partial class DirectoryNameWindow : Window
    {
        public static TreeViewItem MainTreeViewItem;

        //Constructor
        public DirectoryNameWindow()
        {
            InitializeComponent();
        }

        //Button "Create" click
        private void btnCreate_Click(object sender, RoutedEventArgs e)
        {
            MainWindow m = Owner as MainWindow;
            if (txtBxProjectName.Text == "") //If textbox IsEmpty
            {
                MessageBox.Show("Input project name!", "iSmart Creator", MessageBoxButton.OK);
            }

            else
            {
                if (!m.TreeViewMain.Items.IsEmpty)
                {
                    var result = MessageBox.Show("Unsaved project will be lost! Continue?", "iSmart Creator", MessageBoxButton.YesNoCancel);
                    if (result == MessageBoxResult.No || result == MessageBoxResult.Cancel)
                    {
                        return;
                    }
                }

                try
                {
                    var dir = Directory.CreateDirectory("Projects\\" + txtBxProjectName.Text);
                    MainWindow.projectFileDirectory = dir.FullName;

                    var mainWindow = Owner as MainWindow;
                    if (mainWindow != null)
                    {
                        if (!mainWindow.TreeViewMain.Items.IsEmpty)
                        {
                            mainWindow.TreeViewMain.Items.Clear();
                        }

                        MainTreeViewItem = new TreeViewItem { Header = dir.Name };
                        mainWindow.TreeViewMain.Items.Add(MainTreeViewItem);

                        mainWindow.MenuItemAddSpecial.IsEnabled = true;
                        mainWindow.MenuItemCloseAll.IsEnabled = true;
                        mainWindow.MenuItemOpenFile.IsEnabled = true;
                        mainWindow.BtnNew.IsEnabled = true;
                        mainWindow.BtnOpen.IsEnabled = true;
                        this.Close();
                    }
                }
                catch (ArgumentException)
                {
                    MessageBox.Show("Invalid project name!", "Error", MessageBoxButton.OK);
                }
            }
        }
    }
}
